package com.qianli.user.facade.impl.feature;

import com.fqgj.common.api.Response;
import com.google.common.collect.Sets;
import com.qianli.user.application.UserFeatureApplication;
import com.qianli.user.facade.feature.UserFeatureServiceFacade;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.feature.UserFeatureTagRO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * name:用户特征信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 22:03
 * version: v1.0.0
 **/
@Service("userFeatureServiceFacade")
public class UserFeatureServiceFacadeImpl implements UserFeatureServiceFacade{

    private static final Logger LOGGER = LoggerFactory.getLogger(UserFeatureServiceFacade.class);

    @Autowired
    private UserFeatureApplication userFeatureApplication;
    @Override
    public Response<Boolean> saveUserFeatureTags(List<UserFeatureTagRO> userFeatureTags) {
        if(CollectionUtils.isEmpty(userFeatureTags)){
            LOGGER.info("UserFeatureServiceFacade.saveUserFeatureTags param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"参数不能为空");
        }
        Set<String> userCodes = Sets.newHashSet();
        for(UserFeatureTagRO tagRO:userFeatureTags){
            userCodes.add(tagRO.getUserCode());
        }
        if(userCodes.size()>1){
            LOGGER.info("UserFeatureServiceFacade.saveUserFeatureTags batch add tags for multi users is not allowed");
            return Response.error(UserStateCode.FORBID_MULTI_ADD_TAGS_FOR_USERS,"参数不能为空");
        }
        Response<Boolean> response = userFeatureApplication.saveUserFeatureTags(userFeatureTags);
        return response;
    }

    /**
     * 移除标签
     * @param userFeatureTags
     * @return
     */
    @Override
    public Response<Boolean> removeUserFeatureTags(List<UserFeatureTagRO> userFeatureTags) {
        if(CollectionUtils.isEmpty(userFeatureTags)){
            LOGGER.info("UserFeatureServiceFacade.removeUserFeatureTags param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK,"参数不能为空");
        }
        Set<String> userCodes = Sets.newHashSet();
        for(UserFeatureTagRO tagRO:userFeatureTags){
            userCodes.add(tagRO.getUserCode());
        }
        if(userCodes.size()>1){
            LOGGER.info("UserFeatureServiceFacade.removeUserFeatureTags batch remove tags for multi users is not allowed");
            return Response.error(UserStateCode.FORBID_MULTI_ADD_TAGS_FOR_USERS,"参数不能为空");
        }
        Response<Boolean> response = userFeatureApplication.removeUserFeatureTags(userFeatureTags);
        return response;
    }
}
